//
//  ResetPasswordViewController.m
//  DaiShengInvest
//
//  Created by davinci on 17/6/9.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "LoginViewController.h"

@interface ResetPasswordViewController ()

@property (nonatomic, weak)UIImageView * backViewImage;

@property (nonatomic, weak)UIView * backViewOne;

@property (nonatomic, weak)UILabel * telLabel;

@property (nonatomic, weak)UITextField * telTextField;

@property (nonatomic, weak)UIView * lineView;

@property (nonatomic, weak)UIView * backViewTwo;

@property (nonatomic, weak)UILabel * passwordLabel;

@property (nonatomic, weak)UITextField * passwordText;

@property (nonatomic, strong)NSTimer * countDownTimer;

@property (nonatomic, weak)UILabel * explainLable;

@property (nonatomic, weak)UILabel * talNumberLable;


@property (nonatomic, weak)UIButton * resentButton;


@property (nonatomic, weak)UIButton * sureButton;

@end

@implementation ResetPasswordViewController

//倒计时总的秒数
static NSInteger  secondsCountDown = 60;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    secondsCountDown = 60;
    
    //设置定时器
//    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
    [self setupNavBar];
    [self setupUI];
    [self setupDownTimer];
}


- (void)setupDownTimer{
    
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}


//- (void)setTelNumberStr:(NSString *)telNumberStr{
//    
//    _telNumberStr = telNumberStr;
//    
//    self.talNumberLable.text = telNumberStr;
//}


- (void)setupNavBar{
    
    self.title = @"找回密码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLogView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



//定时器的方法
- (void)countDownAction{
    
    secondsCountDown --;
    
    [self.resentButton setTitle:[NSString stringWithFormat:@"%lds后重新获取",(long)secondsCountDown] forState:UIControlStateDisabled];
    
    //当倒计时到0时做需要的操作，button变成可选状态
    if(secondsCountDown == 0){
        
        [_countDownTimer invalidate];
        
        [self.resentButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.resentButton.enabled = YES;
        [self.resentButton setBackgroundColor:ColorWithRGB(0xa92c44)];
        [self.resentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}



- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backViewImage = backImage;
    
    [self.view addSubview:self.backViewImage];
    
    UILabel * explainLable = [UILabel new];
    
    explainLable.text = @"已发送验证码到：";
    explainLable.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    explainLable.textColor = ColorWithRGB(0xdedede);
    
    self.explainLable = explainLable;
    
    [self.view addSubview:self.explainLable];
    
    
    UILabel * talNumberLable = [UILabel new];
    
    talNumberLable.text = self.telNumberStr;
    talNumberLable.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    talNumberLable.textColor = ColorWithRGB(0xdedede);
    
    self.talNumberLable = talNumberLable;
    
    [self.view addSubview:self.talNumberLable];
    
    
    UIView * backViewOne = [[UIView alloc] init];
    
    backViewOne.backgroundColor = [UIColor whiteColor];
    backViewOne.alpha = 0.1;
    
    self.backViewOne = backViewOne;
    
    [self.view addSubview:self.backViewOne];
    
    UILabel * telLabel = [[UILabel alloc] init];
    
    telLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telLabel.textColor = [UIColor whiteColor];//ColorWithRGB(0xeeeeee);
    telLabel.text = @"验证码";
    
    self.telLabel = telLabel;
    
    [self.view addSubview:self.telLabel];
    
    UITextField * telText = [[UITextField alloc] init];
    
    telText.borderStyle = UITextBorderStyleNone;
    telText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    telText.tintColor = ColorWithRGB(0x808486);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x808486);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:attrs];
    telText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telText.textColor = ColorWithRGB(0x808486);
    telText.keyboardType = UIKeyboardTypeNumberPad;
    telText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.telTextField = telText;
    
    [self.view addSubview:self.telTextField];
    
    
    UIButton * resentButton = [[UIButton alloc] init];
    
    resentButton.enabled = NO;
    [resentButton setBackgroundColor:[UIColor clearColor]];
    [resentButton setTitle:@"60s后重新获取" forState:UIControlStateDisabled];
    [resentButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    resentButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [resentButton setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
    [resentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    resentButton.layer.borderColor = [ColorWithRGB(0xa92c44) CGColor];
    
    //边框宽度
    resentButton.layer.borderWidth =1.0f;
    //处理圆角
    resentButton.layer.cornerRadius =5.0f;
    [resentButton addTarget:self action:@selector(didRsentMessageButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.resentButton = resentButton;
    
    [self.view addSubview:self.resentButton];
    
    
    UIView * lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = ColorWithRGB(0x526366);
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
    UIView * backViewTwo = [[UIView alloc] init];
    
    backViewTwo.backgroundColor = [UIColor whiteColor];
    backViewTwo.alpha = 0.1;
    
    self.backViewTwo = backViewTwo;
    
    [self.view addSubview:self.backViewTwo];
    
    UILabel * passwordLabel = [[UILabel alloc] init];
    
    passwordLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    passwordLabel.textColor = ColorWithRGB(0xeeeeee);
    passwordLabel.text = @"密码";
    
    
    self.passwordLabel = passwordLabel;
    
    [self.view addSubview:self.passwordLabel];
    
    
    UITextField * passwordText = [[UITextField alloc] init];
    
    passwordText.borderStyle = UITextBorderStyleNone;
    passwordText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    passwordText.tintColor = ColorWithRGB(0x808486);
    
    passwordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"至少6位，须包含大小写字母及数字" attributes:attrs];
    passwordText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    passwordText.textColor = ColorWithRGB(0x808486);
//    passwordText.keyboardType = UIKeyboardTypeNumberPad;
    passwordText.keyboardAppearance = UIKeyboardAppearanceDark;
    passwordText.secureTextEntry = YES;
    
    self.passwordText = passwordText;
    
    [self.view addSubview:self.passwordText];
    
    
    
    UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];//[UIFont systemFontOfSize:15];
//    [sureButton setBackgroundColor:ColorWithRGB(0xa92c44)];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    sureButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    sureButton.layer.borderWidth =1.0f;
    //处理圆角
    sureButton.layer.cornerRadius =5.0f;
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didSureButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.backViewImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.backViewImage);
    }];
    
    [self.explainLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(81 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(24 * SCREENH6);
    }];
    
    
    [self.talNumberLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.explainLable);
        make.leading.mas_equalTo(self.explainLable.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.backViewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
        make.top.mas_equalTo(self.talNumberLable.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backViewOne).offset(9 * SCREENH6);
        make.centerY.mas_equalTo(self.backViewOne);
        make.width.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.telTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backViewOne);
        make.leading.mas_equalTo(self.telLabel.mas_trailing).offset(42 * SCREENH6);
        make.height.mas_equalTo(self.telLabel);
        make.width.mas_equalTo(125 * SCREENH6);
    }];
    
    [self.resentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backViewOne);
        make.trailing.mas_equalTo(self.backViewOne).offset(- 12 * SCREENH6);
        make.width.mas_equalTo(98 * SCREENH6);
        make.height.mas_equalTo(33 * SCREENH6);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backViewOne.mas_bottom);
        make.leading.trailing.mas_equalTo(self.backViewOne);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.backViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.backViewOne);
        make.top.mas_equalTo(self.lineView.mas_bottom);
    }];
    
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.telLabel);
        make.centerY.mas_equalTo(self.backViewTwo);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.mas_equalTo(self.telTextField);
//        make.width.mas_equalTo(200 * SCREENH6);
        make.trailing.mas_equalTo(self.lineView);
        make.centerY.mas_equalTo(self.backViewTwo);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backViewTwo.mas_bottom).offset(55 * SCREENH6);
        make.leading.width.mas_equalTo(self.backViewTwo);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
}



- (void)didSureButton{
    
    if (self.passwordText.text) {
        
        NSDictionary * parametersDic = @{@"phone":self.talNumberLable.text,@"password":self.passwordText.text,@"code":self.telTextField.text,@"rePassword":self.passwordText.text};
        
        [[NetworkTool sharedTools] POST:@"user/forget-pass" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSString * str = responseObject[@"msg"];
            
            if ([str  isEqual: @""]) {
                
                NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
                
                [userDefaults setObject:self.passwordText.text forKey:@"password"];
                
                [userDefaults synchronize];
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"修改密码成功！立即登录！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
//                                                                          NSLog(@"action = %@", action);
                                                                          [self.navigationController pushViewController:[LoginViewController new] animated:YES];
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:str
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请输入密码！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
    
}



- (void)didRsentMessageButton{
    
//    NSLog(@"huhaha");
    
    self.resentButton.enabled = NO;
    self.resentButton.layer.borderColor = [ColorWithRGB(0xa92c44) CGColor];
    [self.resentButton setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
    [self.resentButton setTitle:@"60s后重新获取" forState:UIControlStateDisabled];
    [self.resentButton setBackgroundColor:[UIColor clearColor]];
    secondsCountDown = 60;
    [self setupDownTimer];
    
    NSDictionary * parametersDic = @{@"phone":self.telTextField.text,@"cate":@"2"};
    
    
    [[NetworkTool sharedTools] POST:@"site/sms-very" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


//POP回上一个界面
- (void)backToLogView{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)viewDidDisappear:(BOOL)animated{
    
    [_countDownTimer invalidate];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.telTextField resignFirstResponder];
    [self.passwordText resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
